package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.course.domain.ProjectMinutesCheckDomain;
import com.aizhixin.lab.project.course.domain.ProjectWeeklyDomain;
import com.aizhixin.lab.project.course.domain.QueryProjectWeeklyDomain;
import com.aizhixin.lab.project.course.entity.ProjectGroup;
import com.aizhixin.lab.project.course.entity.ProjectWeekly;
import com.aizhixin.lab.project.course.service.ProjectWeeklyService;
import com.aizhixin.lab.project.course.vo.ProjectWeeklyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/project/weekly")
@RestController
@Api(value = "项目周报", tags = "项目周报")
public class ProjectWeeklyController {
    @Autowired
    private ProjectWeeklyService projectWeeklyService;
    @Autowired
    private AccountService accountService;
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ProjectWeeklyDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            ProjectWeekly save = projectWeeklyService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save.getId());
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> update(@RequestHeader("Authorization") String token, @RequestBody ProjectWeeklyDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (!account.getId().equals(domain.getCreatedBy())){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.MESSAGE,"只允许本人修改自己的纪要内容");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        try{
            ProjectWeekly save = projectWeeklyService.update(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save.getId());
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectWeekly", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "周报查询", httpMethod = "POST", notes = "周报查询（添加createdBy表示查询当前登录账号的周报，不加则查询该小组所有人的周报）")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response =  ProjectWeeklyVo.class),})
    public ResponseEntity<?> findProject(@RequestHeader("Authorization") String token, @RequestBody QueryProjectWeeklyDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            Map<String, Object> projectMinutesVoPageData = projectWeeklyService.findProjectWeekly(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,projectMinutesVoPageData);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return   new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "周报审核", httpMethod = "POST", notes = "周报审核")
    public ResponseEntity<?> check(@RequestHeader("Authorization") String token, @RequestBody ProjectMinutesCheckDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            ProjectWeekly save = projectWeeklyService.check(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save.getId());
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

}
